package com.bsoft.gol.hcs.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class SM4Utils
{
	private static String secretKey ="5ff7db748e839782";

	private static String iv = "UISwD9fW6cFh9SNS";
	private static String CHARSET_UTF8 = "UTF-8";

	private static boolean hexString = false;

	public SM4Utils()
	{
	}

	public static String encryptData_ECB(String plainText)
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = true;
			ctx.mode = SM4.SM4_ENCRYPT;

			byte[] keyBytes;
			if (hexString)
			{
				keyBytes = Util.hexStringToBytes(secretKey);
			}
			else
			{
				keyBytes = secretKey.getBytes();
			}

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_enc(ctx, keyBytes);
			byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainText.getBytes(CHARSET_UTF8));
			String cipherText = new BASE64Encoder().encode(encrypted);
			if (cipherText != null && cipherText.trim().length() > 0)
			{
				Pattern p = Pattern.compile("\\s*|\t|\r|\n");
				Matcher m = p.matcher(cipherText);
				cipherText = m.replaceAll("");
			}
			return cipherText;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public static String decryptData_ECB(String cipherText)
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = true;
			ctx.mode = SM4.SM4_DECRYPT;

			byte[] keyBytes;
			if (hexString)
			{
				keyBytes = Util.hexStringToBytes(secretKey);
			}
			else
			{
				keyBytes = secretKey.getBytes();
			}

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_dec(ctx, keyBytes);


			byte[] decrypted = sm4.sm4_crypt_ecb(ctx, new BASE64Decoder().decodeBuffer(cipherText));
			return new String(decrypted, CHARSET_UTF8);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public static String encryptData_CBC(String plainText)
	{
		if(plainText==null || "".equals(plainText)){
			return null;
		}
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = true;
			ctx.mode = SM4.SM4_ENCRYPT;

			byte[] keyBytes;
			byte[] ivBytes;
			if (hexString)
			{
				keyBytes = Util.hexStringToBytes(secretKey);
				ivBytes = Util.hexStringToBytes(iv);
			}
			else
			{
				keyBytes = secretKey.getBytes();
				ivBytes = iv.getBytes();
			}

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_enc(ctx, keyBytes);
			byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, plainText.getBytes(CHARSET_UTF8));
			String cipherText = new BASE64Encoder().encode(encrypted);
			if (cipherText != null && cipherText.trim().length() > 0)
			{
				Pattern p = Pattern.compile("\\s*|\t|\r|\n");
				Matcher m = p.matcher(cipherText);
				cipherText = m.replaceAll("");
			}
			return cipherText;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public static String decryptData_CBC(String cipherText)
	{
		if(cipherText==null){
			return null;
		}
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = true;
			ctx.mode = SM4.SM4_DECRYPT;

			byte[] keyBytes;
			byte[] ivBytes;
			if (hexString)
			{
				keyBytes = Util.hexStringToBytes(secretKey);
				ivBytes = Util.hexStringToBytes(iv);
			}
			else
			{
				keyBytes = secretKey.getBytes();
				ivBytes = iv.getBytes();
			}

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_dec(ctx, keyBytes);
			byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, new BASE64Decoder().decodeBuffer(cipherText));
			return new String(decrypted, CHARSET_UTF8);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return cipherText;
		}
	}

	public static void main(String[] args)
	{
		String plainText = "{\"id\":\"61972fa3ec41fe00a0dc9c1d\",\"roleId\":\"618a11f54ded5e009081bf0a\",\"userId\":\"618a11f54ded5e009081bf0b\",\"skinName\":\"\"}";
		System.out.println("手机号码明文: " + plainText);

		SM4Utils.secretKey = "5ff7db748e839782";
		SM4Utils.hexString = false;

		System.out.println("SM4 CBC加密模式");
		SM4Utils.iv = "UISwD9fW6cFh9SNS";
		String cipherText = SM4Utils.encryptData_ECB(plainText);
		System.out.println("加密结果: " + cipherText);
		plainText=null;
		plainText = SM4Utils.decryptData_ECB("H6OVFcHnPHGt3RW9aPrdZqFrwIr25feqn6O4qgbOUe8/eGj+waUKPB2nGEl726gWSW7QFXD/9oWEe4RWioxQjoplauUcTv7+uM9ViHLFd2qb7Pv+6rlp4W3jee6mJFtWdqzhBN4MekxqdoSvUuPsGyLpkQmXsB1nvVhdrJiBon8==");
		System.out.println("解密结果: " + plainText);
	}

	public static void main2(String[] args)
	{
		String plainText = " {\"id\":\"61972fa3ec41fe00a0dc9c1d\",\"roleId\":\"618a11f54ded5e009081bf0a\",\"userId\":\"618a11f54ded5e009081bf0b\",\"skinName\":\"\"}";
		System.out.println("手机号码明文: " + plainText);

		SM4Utils.secretKey = "5ff7db748e839782";
		SM4Utils.hexString = false;

		System.out.println("SM4 CBC加密模式");
		SM4Utils.iv = "UISwD9fW6cFh9SNS";
		String cipherText = SM4Utils.encryptData_CBC(plainText);
		System.out.println("加密结果: " + cipherText);
		plainText=null;
		plainText = SM4Utils.decryptData_CBC(cipherText);
		System.out.println("解密结果: " + plainText);
	}
}
